<?php
/**
 * 用户管理
 */

namespace app\tdadmin\controller;

use app\common\controller\AdminBaseController;
use app\common\model\MemberModel;
use think\Db;

class MemberController extends AdminBaseController
{
    public function index()
    {
        return $this->fetch();

    }

    public function getUsers()
    {
        $mobile = $this->request->param('key.phone', '');
        $map[] = ['phone', 'like', '%' . $mobile . '%'];

        $count = MemberModel::where($map)->order('id desc')->count();

        $page = $this->request->param('page/d', 1, 'intval');
        $limit = $this->request->param('limit/d', 10, 'intval');

        $lists = MemberModel::where($map)->order("id desc")->limit(($page - 1) * $limit, $limit)->all();

        $res['code'] = 0;
        $res['msg'] = 'success';
        $res['data'] = $lists;
        $res['count'] = $count;
        return $res;
    }


    public function add()
    {
        if ($this->request->isPost()) {
            $res['code'] = 1;
            $data = $this->request->param('');

            if ($data['login_pass'] != $data['upass']) {
                $res['msg'] = '两次密码输入不一致';
            }

            $name = $data['nick_name'];
            $phone = $data['phone'];

            $info = Db::name("member")->where('nick_name', $name)->count();
            $phone = Db::name("member")->where('phone', $phone)->count();

            if ($info) {
                $res['code'] = 0;
                $res['msg'] = '该名称已被占用';
            } else if ($phone) {
                $res['code'] = 0;
                $res['msg'] = '该手机号已被占用';
            } else {
                $data['password'] = password($data['login_pass']);
                unset($data['upass']);
                unset($data['login_pass']);
                $admin_id = Db::name('member')->insertGetId($data);
                addLog("添加了新用户ID:".$admin_id,Db::getLastSql());

                if ($admin_id) {
                    $res['msg'] = '添加成功';
                }
            }
            return $res;
        } else {

            $roles = Db::name("auth_group")->where("status", 1)
                ->order("id desc")
                ->all();

            $this->assign("roles", $roles);

            return $this->fetch();
        }
    }


    public function edit()
    {
        if ($this->request->isPost()) {
            $data = $this->request->param('');

            $result = Db::name("member")->update($data);
            addLog("修改了用户信息:id".$data['id'],Db::getLastSql());

            if ($result !== false) {
                $this->success("修改成功");
            } else {
                $this->error("修改失败");
            }
        } else {
            $id = input("param.id");

            $data = Db::name("member")->get($id);

            $roles = Db::name("auth_group")->where("status = 1")->all();

            $this->assign(["roles" => $roles, "data" => $data]);

            return $this->fetch();
        }
    }

    public function delAdmin()
    {
        $id = input("param.id");

        Db::name("member")->delete($id);
        addLog("删除了用户信息:id".$id,Db::getLastSql());

        $this->success("删除成功");
    }
}